package com.selen.controller;

import cn.hutool.core.bean.BeanUtil;
import com.selen.dto.*;
import com.selen.entities.Dept;
import com.selen.entities.Emp;
import com.selen.excel.DeptExcel;
import com.selen.excel.EmpExcel;
import com.selen.result.Result;
import com.selen.service.EmpService;
import com.selen.utils.EasyExcelUtil;
import com.selen.utils.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Selen
 */
@Tag(name = "员工模块")
@RestController
@RequestMapping("/api/v1/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Operation(summary = "插入一条记录")
    @PostMapping("/save")
    public Object save(@RequestBody @Validated EmpInsertDTO dto){
        return empService.insert(dto);
    }

    @Operation(summary = "查询一条记录")
    @GetMapping("/getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return empService.selectById(id);
    }

    @Operation(summary = "修改一条记录")
    @PostMapping("/update")
    public Object update(@RequestBody @Validated EmpUpdateDTO dto){
        return empService.updateById(dto);
    }

    @Operation(summary = "删除一条记录")
    @PostMapping("/remove/{id}")
    public Object update(@PathVariable Long id){
        return empService.logicDeleteById(id);
    }

    @Operation(summary = "批量删除记录")
    @PostMapping("/removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){
        return empService.logicDeleteByIds(ids);
    }

    @Operation(summary = "查询全部记录")
    @GetMapping("/list")
    public Object list(){
        return empService.selectAll();
    }

    @Operation(summary = "分页查询记录")
    @PostMapping("/page")
    public Object page(@RequestBody @Validated PageDTO dto){
        return empService.page(dto);
    }

    @Operation(summary = "账号密码登录")
    @PostMapping("/loginByAccount")
    public Object loginByAccount(@RequestBody @Validated LoginDTO dto){
        return empService.login(dto);
    }

    @Operation(summary = "名称模糊查询")
    @PostMapping("/dimSelect")
    public Object dimSelect(@RequestBody @Validated EmpPageDTO dto){
        return empService.dimSelect(dto);
    }

    @SneakyThrows
    @Operation(summary = "打印数据")
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<EmpExcel> empExcels = new ArrayList<>();
        List<Emp> emps = empService.selectAll();
        for (Emp emp : emps) {
            EmpExcel empExcel = BeanUtil.copyProperties(emp, EmpExcel.class);
            empExcel.setDeptTitle(emp.getDept().getTitle());
            empExcels.add(empExcel);
        }
        EasyExcelUtil.download(resp, "员工统计表.xlsx", "sheet01",
                DeptExcel.class, empExcels);
    }

    @Operation(summary = "头像上传")
    @PostMapping("/uploadAvatar")
    public Object uploadAvatar(EmpUploadDTO dto){
        return new Result<>(empService.uploadAvatar(dto));
    }

    @Operation(summary = "用户密码重置")
    @PostMapping("/resetPassword/{id}")
    public Object resetPassword(@PathVariable Long id){
        return new Result<>(empService.resetPassword(id));
    }

    @Operation(summary = "获取用户角色")
    @GetMapping("/getRoles/{id}")
    public Object getRoles(@PathVariable Long id){
        return new Result<>(empService.getRoles(id));
    }

    @Operation(summary = "修改用户角色")
    @PostMapping("/updateRoles")
    public Object updateRoles(@RequestBody EmpRoleDTO dto){
        return new Result<>(empService.updateRoles(dto));
    }

    @Operation(summary = "修改我的密码")
    @PostMapping("/updatePassword")
    public Object updatePassword(@RequestBody EmpUpdatePasswordDTO dto,
                                 @RequestHeader("token") String token){
        dto.setId(JWTUtil.getId(token));
        return empService.updatePassword(dto);
    }

}
